
var LoyaltyCoupons = {
    
    init: function() {
        this.bindActions();
        this.setupDataTable();
    },
    
    bindActions: function() {
        this.bindGenerateAction();
        this.bindEditAction();
        this.bindDeleteAction();
    },
    
    bindGenerateAction: function() {
        var self = this;
        
        $("#create_coupon_btn").click(function() {
            var url = $(this).data('url');
            
            $.ajax({
                url: url,
                type: 'post',
                success: function(data) {
                    Jlib.showDialog({
                        title: 'Create New Coupon',
                        content: data,
                        buttons: {
                            'Create': function() {
                                Jlib.submitDialog();
                            },
                            'Cancel': function() {
                                Jlib.closeDialog();
                            }
                        },
                        close: function(data) {
                            self.createCouponRecord(data);
                        }
                    });
                },
                error: function(xhr) {
                    console.log(xhr.responseText);
                }
            });
        });
    },
    
    bindEditAction: function() {
        var self = this;
        
        $(".edit_link").click(function(e) {
            e.preventDefault();
            
            var url = $(this).attr('href');
            
            $.ajax({
                url: url,
                type: 'get',
                success: function(data) {
                    Jlib.showDialog({
                        title: 'Edit Coupon',
                        content: data,
                        buttons: {
                            'Save Changes': function() {
                                Jlib.submitDialog();
                            },
                            'Cancel': function() {
                                Jlib.closeDialog();
                            }
                        },
                        close: function(data) {
                            self.createCouponRecord(data);
                        }
                    });
                },
                error: function(xhr) {
                    console.log(xhr.responseText);
                }
            });
        });
    },
    
    bindDeleteAction: function() {
        $(".delete_link").click(function(e) {
            e.preventDefault();
            
            var url = $(this).attr("href");
            var row = $(this).parents("tr");
            
            $.msgAlert ({
                type: 'warning', 
                title: 'Confirm Coupon Deletion', 
                text: 'Are you sure you want to delete this coupon?',
                callback: function() {
                    $.get(url, function() {
                        row.remove();
                    });
                }
            });
        });
    },
    
    setupDataTable: function() {
        var self = this;
        
        $(".datatable tbody tr").click(function() {
            $(".datatable tbody tr").removeClass("selected");
            $(".datatable tbody tr").find("input[type=checkbox]").removeAttr("checked", "checked");
            
            $(this).addClass("selected");
            $(this).find("input[type=checkbox]").attr("checked", "checked");
            
            var url = $(this).data('url');
            
            $.get(url, function(data) {
                $("#coupon_details").html(data);
                
                self.bindCampaignLinks();
            });
        });
    },
    
    bindCampaignLinks: function() {
        $("#coupon_details .add_campaign").click(function(e) {
            e.preventDefault();
            
            var url = $(this).attr("href");
            
            $.ajax({
                url: url,
                type: 'post',
                success: function(data) {
                    Jlib.showDialog({
                        title: 'Create Campaign',
                        content: data,
                        height: 500,
                        buttons: {
                            'Create Campaign': function() {
                                Jlib.submitDialog();
                            },
                            'Cancel': function() {
                                Jlib.closeDialog();
                            }
                        },
                        close: function() {
                            window.history.go(0);
                        }
                    });
                },
                error: function(xhr) {
                    console.log(xhr.responseText);
                }
            });
        });
    },
    
    createCouponRecord: function(data) {
        window.history.go(0);
    }
};

$(document).ready(function() {
    LoyaltyCoupons.init();
});